<template>
  <view class="body">
    <view class="name titleBold">您的肾脏功能检查结果（非必填）</view>
    <view>
      <view class="inputBox">
        <span class="name lineHeight86">尿微量白蛋白</span>
        <input
          id="MAU"
          v-model="mainData.MAU"
          type="digit"
          class="uni-input lineHeight86 marginR120"
          name="MAU"
          placeholder-class="phcolor"
          @input="inputChange('MAU')"
        />
        <view class="unit">mg/l</view>
        <view class="clear"></view>
      </view>
      <view class="inputBox">
        <span class="name lineHeight86">尿酸</span>
        <input
          id="uricAcid"
          v-model="mainData.uricAcid"
          type="digit"
          class="uni-input lineHeight86 marginR120"
          name="uricAcid"
          placeholder-class="phcolor"
          @input="inputChange('uricAcid')"
        />
        <view class="unit">umol/L</view>
        <view class="clear"></view>
      </view>
      <view class="inputBox uniPostion">
        <span class="name lineHeight86">血肌酐</span>
        <input
          v-model="mainData.Scr"
          type="digit"
          class="uni-input lineHeight86 marginR120"
          name="Scr"
          placeholder-class="phcolor"
          @input="inputChange('Scr')"
        />
        <view class="unit">
          <span v-show="newScrUnit === 1">umol/l</span>
          <span v-show="newScrUnit === 2">mg/dl</span>
          <!-- <picker :range="array" :value="index" @change="bindPickerChange">
              <view class="uni-input">{{ array[index] }}</view>
            </picker> -->
        </view>
        <view class="clear"></view>
      </view>
      <view class="inputBox unitBox">
        <view class="name lineHeight86">选择血肌酐单位</view>
        <view class="unit">
          <span
            v-for="(item, index) in array"
            :key="index"
            :class="['unitList', newScrUnit === item.id ? 'cur' : null]"
            @click="choseUnit(item)"
            >{{ item.name }}
            <span v-if="newScrUnit === item.id" class="curImg"><img :src="curIcon" /></span>
          </span>
          <!-- <span class="unitList">mg/dl</span> -->
        </view>
        <view class="clear"></view>
      </view>
    </view>
    <!-- <view v-else class="tips"> 题目录入已完成，请点击提交 </view> -->
    <!-- 下一题上一题按钮 -->
  </view>
</template>

<script>
import { checkInt, checkBetween, decimalTwo } from '@/common/assessKeyCheck';

// import { getData } from '@/utils/optimize';
// import { ajaxSend } from './ajaxHealth.js';
import { mapGetters } from 'vuex';
import curIcon from '@/static/icon/cur.png';
export default {
  data() {
    return {
      mainData: {
        MAU: '',
        uricAcid: '',
        Scr: ''
      },
      array: [
        { id: 1, name: 'umol/L' },
        { id: 2, name: 'mg/dl' }
      ],
      index: 0,
      newScrUnit: 1,
      curIcon: curIcon
    };
  },
  computed: {
    ...mapGetters(['active', 'assessForm', 'isHaveDiabetes'])
  },
  mounted() {},
  methods: {
    validate() {
      // 先校验数据
      let isPassAuth = true;
      // ① 必填校验
      // ②录入值格式校验
      if (checkBetween(this.mainData.MAU, '尿微量白蛋白', 0.01, 2000)) {
        uni.showToast({
          title: `尿微量白蛋白数据错误，请重新输入`,
          icon: 'none'
        });
        return false;
      }
      if (this.mainData.uricAcid && checkInt(this.mainData.uricAcid)) {
        uni.showToast({ title: `尿酸数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      if (checkBetween(this.mainData.uricAcid, '尿微量白蛋白', 30, 5000)) {
        uni.showToast({ title: `尿酸数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      // 血肌酐校验
      if (decimalTwo(this.mainData.Scr)) {
        uni.showToast({ title: `血肌酐数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      let xjgChecke = false;
      // if (this.index === '1') {
      if (this.newScrUnit === 2) {
        xjgChecke = checkBetween(this.mainData.Scr, '血肌酐', 0.06, 56);
      } else {
        xjgChecke = checkBetween(this.mainData.Scr, '血肌酐', 5, 5000);
      }
      if (xjgChecke) {
        uni.showToast({ title: `血肌酐数据错误，请重新输入`, icon: 'none' });
        return false;
      }
      if (!isPassAuth) {
        return false;
      }
      // 提交vuex assessForm值
      let submitData = this.assessForm;
      Object.keys(this.mainData).forEach((key) => {
        submitData[key] = this.mainData[key];
      });
      this.mainData.scrUnit = this.newScrUnit;
      return this.mainData;
      // 血肌酐单位
      // submitData.scrUnit = this.index - 0 + 1;
      // submitData.scrUnit = this.newScrUnit;
      // submitData.physicalExaminationDate = getData();

      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 start
      // const checkKeyIsInput = [
      //   {
      //     nameKey: 'MAU',
      //     title: '尿微量白蛋白',
      //     healthGuidances: ['信息缺失，无法给出评估结论']
      //   },
      //   {
      //     nameKey: 'uricAcid',
      //     title: '尿酸',
      //     healthGuidances: ['信息缺失，无法给出评估结论']
      //   },
      //   {
      //     nameKey: 'Scr',
      //     title: '血肌酐',
      //     healthGuidances: ['信息缺失，无法给出评估结论']
      //   }
      // ];
      // let healthData = await ajaxSend(this.mainData);
      // Object.keys(this.mainData).forEach((key) => {
      //   submitData[key] = this.mainData[key];
      //   if (!this.mainData[key]) {
      //     healthData.push(...checkKeyIsInput.filter((item) => item.nameKey === key));
      //   }
      // });
      // let curActive = this.active + 1;
      // if (healthData && healthData.length > 0) {
      //   // 如果有异常，则提示
      //   this.$store.commit('assessQuestion/SET_AJAXRESMESSAGE', healthData); // 存提示信息到vux，数组格式
      //   this.$store.commit('assessQuestion/SET_AJAXRES', true); // 开启提示
      // } else {
      //   this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
      //   this.$store.commit('assessQuestion/SET_AJAXRES', false);
      // }
      // this.$store.commit('assessQuestion/SET_ASSESSFORM', submitData);

      // 获取健康指标指导意见 判断是出健康提示还是直接下一步 end
    },
    proBtn() {
      let curActive = this.active - 1;
      this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
    },
    bindPickerChange: function (e) {
      this.index = e.target.value;
    },
    choseUnit(val) {
      val.id === 1 ? (this.newScrUnit = 1) : (this.newScrUnit = 2);
      this.$emit('choseList', 'scrUnit', this.newScrUnit);
    },
    inputChange(key) {
      this.$emit('choseList', key, this.mainData[key]);
    }
  }
};
</script>
